#include "util.hpp"
#include "config.hpp"
#include "data.hpp"
#include "hot.hpp"
#include "service.hpp"
#include <thread>
void Test(const std::string &filename)
{
    // cloud::FileUtil fu(filename);

    // int64_t size = fu.FileSize();  //文件大小
    // time_t AccessTime = fu.LastAtime();
    // time_t ModifyTime = fu.LastMTime();
    // std::string name = fu.FileName();

    // std::cout<<"file_size: "<<size<<std::endl;
    // std::cout<<"AccessTime: "<<AccessTime<<std::endl;
    // std::cout<<"ModifyTime: "<<ModifyTime<<std::endl;
    // std::cout<<"name: "<<name<<std::endl;

    // //读取文件内容，写入到另外一个文件去
    // cloud::FileUtil fu(filename);
    // std::string body;
    // fu.GetContent(&body);

    // cloud::FileUtil nuf("./hello.txt");
    // nuf.SetContent(&body);
    // cloud::FileUtil fu(filename);
    // fu.Compress("comp_hello.txt");

    // std::cout<<"压缩成功:comp_hello.txt\n";

    // cloud::FileUtil ufu("comp_hello.txt");
    // ufu.UnCompress("uncom_cloud.cpp");
    // std::cout<<"解压缩成功:uncom_cloud.cpp\n";

    cloud::FileUtil fu(filename);
    fu.CreateDirectory();
    std::vector<std::string> arry;
    fu.ScanDirectory(&arry);
    for (auto &p : arry)
    {
        std::cout << p << std::endl;
    }

    return;
}

void JsonUtilTest()
{
    const char *name = "小明";
    int age = 19;
    float score[3] = {88.5, 99, 58};
    Json::Value val;
    val["姓名"] = name;
    val["年龄"] = age;
    val["成绩"].append(score[0]);
    val["成绩"].append(score[1]);
    val["成绩"].append(score[2]);
    std::string json_str;
    cloud::JsonUtil::Serialize(val, &json_str);
    std::cout << json_str << std::endl;

    Json::Value unval;
    cloud::JsonUtil::UnSerialize(json_str, &unval);
    std::cout << unval["姓名"].asString() << std::endl;
    std::cout << unval["年龄"].asInt() << std::endl;
    for (int i = 0; i < val["成绩"].size(); i++)
    {
        std::cout<<val["成绩"][i].asFloat()<<std::endl;
    }
    
    return;
}

void ConfigTest()
{
     cloud::Config* config = cloud::Config::GetInstance();
     std::cout<<config->GetHotTime()<<std::endl;
     std::cout<<config->GetServerPort()<<std::endl;
     std::cout<<config->GetServerIP()<<std::endl;
     std::cout<<config->GetBackDir()<<std::endl;
     std::cout<<config->GetBackupFile()<<std::endl;
     std::cout<<config->GetDownloadPrefix()<<std::endl;
     std::cout<<config->GetPackDir()<<std::endl;
     std::cout<<config->GetPackFileSuffix()<<std::endl;    
}

void DataTest(const std::string &realpath)
{
    cloud::DataManager data;   //加载所有数据
    std::vector<cloud::BackupInfo> arry;
    data.GetAll(&arry);

    for(auto &a : arry)
    {
        std::cout<<a.pack_flag<<std::endl;
        std::cout<<a.mtime<<std::endl;
        std::cout<<a.atime<<std::endl;
        std::cout<<a.fsize<<std::endl;
        std::cout<<a.real_path<<std::endl;
        std::cout<<a.pack_path<<std::endl;
        std::cout<<a.url<<std::endl;
    }

    // std::cout<<"------------insert and GetOneByUrl-------"<<std::endl;
    // cloud::BackupInfo bpi;
    // bpi.NewBackupInfo(realpath);
    // cloud::DataManager data;
    // data.Insert(bpi);

    // cloud::BackupInfo tem;
    // data.GetOneByUrl("/download/bundle.h", &tem);
    // std::cout<<tem.pack_flag<<std::endl;
    // std::cout<<tem.mtime<<std::endl;
    // std::cout<<tem.atime<<std::endl;
    // std::cout<<tem.fsize<<std::endl;
    // std::cout<<tem.real_path<<std::endl;
    // std::cout<<tem.pack_path<<std::endl;
    // std::cout<<tem.url<<std::endl;


    // std::cout<<"------------Update and GetAll-------"<<std::endl;
    // bpi.pack_flag = true;
    // data.Update(bpi);
    // std::vector<cloud::BackupInfo> arry;
    // data.GetAll(&arry);

    // for(auto &a : arry)
    // {
    //     std::cout<<a.pack_flag<<std::endl;
    //     std::cout<<a.mtime<<std::endl;
    //     std::cout<<a.atime<<std::endl;
    //     std::cout<<a.fsize<<std::endl;
    //     std::cout<<a.real_path<<std::endl;
    //     std::cout<<a.pack_path<<std::endl;
    //     std::cout<<a.url<<std::endl;
    // }


    // std::cout<<"------------GetOneByUrl and GetAll-------"<<std::endl;
    // data.GetOneByRealpath(realpath, &tem);

    // std::cout<<tem.pack_flag<<std::endl;
    // std::cout<<tem.mtime<<std::endl;
    // std::cout<<tem.atime<<std::endl;
    // std::cout<<tem.fsize<<std::endl;
    // std::cout<<tem.real_path<<std::endl;
    // std::cout<<tem.pack_path<<std::endl;
    // std::cout<<tem.url<<std::endl;



}

cloud::DataManager *_data;
void HotTest()
{
    // _data = new cloud::DataManager();
    // perror("new DataManager");
    cloud::HotManager hot;
    hot.RunModule();
}


void ServiceTest()
{
    cloud::Service service;
    service.RunModule();
}

int main(int argc, char *argv[])
{
    _data = new cloud::DataManager();
    //Test(argv[1]);
    // JsonUtilTest();
    // ConfigTest();
    // DataTest(argv[1]);
    // HotTest();
    // ServiceTest();
    std::thread hot_thread(HotTest);
    std::thread service_thread(ServiceTest);
    hot_thread.join();
    service_thread.join();

    return 0;
}